<?php
declare(strict_types=1);
namespace App\Controller;
use App\Helper\Utils;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;

/**
 * @AutoController(prefix="api-test")
 */
class ApiTestController extends AbstractController
{
    public function index()
    {
        $http = new \GuzzleHttp\Client();
        $params = [
            'mobile' => '18669387024',
            'type' => 1,
            'code' => 1
        ];
        $public_params = $this->createSign();
        $params = array_merge($params,$public_params);
        $response = $http->post(env('IMAGE_PREFIX').'/api/v1/common/sms/send', [
            'form_params' => $params,
            'headers' =>[
                'Content-Type' => 'application/x-www-form-urlencoded'
            ]
        ]);
        $result = json_decode((string)$response->getBody(), true);
        return $result;
    }

    public function login(){
        $http = new \GuzzleHttp\Client();
        $params = [
            'mobile' => '18669387024',
            'code' => '724961'
        ];
        $public_params = $this->createSign();
        $params = array_merge($params,$public_params);
        $response = $http->post(env('IMAGE_PREFIX').'/api/v1/auth/login/phone', [
            'form_params' => $params,
            'headers' =>[
                'Content-Type' => 'application/x-www-form-urlencoded'
            ]
        ]);
        $result = json_decode((string)$response->getBody(), true);
        return $result;
    }

    private function createSign(){
        $wait_check_params= [
            'app_id' => '20210602171056',
            'nonce_str' => Utils::createNonceStr(),
            'timestamps' => time()
        ];
        $tmps = array();
        foreach ($wait_check_params as $k => $v) {
            $tmps[] = $k . $v;
        }
        $string = 'BaJfpnccSHgEjuNNAFHdfsNw2GNZcU2N' . implode('', $tmps);
        $sign = strtoupper(md5($string));
        $wait_check_params['sign'] = $sign;
        return $wait_check_params;
    }
}